package dormouse.vfs.ui.renderers;

import java.awt.Component;
import java.io.IOException;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.apache.commons.vfs2.FileObject;

import dormouse.vfs.metadata.IMetadataReader;
import dormouse.vfs.metadata.MetadataReaderFactory;

/**
 * This class is used to render file metadata.
 *
 */
public class MetadataRenderer extends JPanel implements IFileRenderer {

    /** Serial Version UID. */
	private static final long serialVersionUID = -5264681820351459720L;
	
	private JScrollPane jScrollPane = null;
    private JTable metadataTable = null;
    private MetadataTableModel model = null;

    /**
     * This method initializes 
     * 
     */
    public MetadataRenderer() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
        this.setSize(new java.awt.Dimension(465, 269));
        this.setBorder(BorderFactory.createTitledBorder(null, "Properties", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
        this.add(getJScrollPane(), null);

    }

    /**
     * This method initializes jScrollPane	
     * 	
     * @return javax.swing.JScrollPane	
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setPreferredSize(new java.awt.Dimension(452, 250));
            jScrollPane.setViewportView(getMetadataTable());
        }
        return jScrollPane;
    }

    /**
     * This method initializes metadataTable	
     * 	
     * @return javax.swing.JTable	
     */
    private JTable getMetadataTable() {
        if (metadataTable == null) {
            metadataTable = new JTable();
            this.model = new MetadataTableModel();
            metadataTable.setModel(this.model);
        }
        return metadataTable;
    }

    public void render(FileObject file) {
    	IMetadataReader reader = MetadataReaderFactory.getInstance(file);
    	try {
			Map<String, String> metadata = reader.getMetadata(file);
			this.model = new MetadataTableModel(metadata);
			metadataTable.setModel(this.model);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    public String[] getFileExtensions() {
        return null;
    }

    public Component getComponent() {
        return this;
    }
}  //  @jve:decl-index=0:visual-constraint="10,10"
